package com.ruoyi.system.service.impl;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.system.domain.TmcActivityUser;
import com.ruoyi.system.domain.TmcUser;
import com.ruoyi.system.mapper.TmcActivityUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TmcCommunityActivityMapper;
import com.ruoyi.system.domain.TmcCommunityActivity;
import com.ruoyi.system.service.ITmcCommunityActivityService;

/**
 * 活动信息Service业务层处理
 *
 * @author ruoyi
 * @date 2021-03-31
 */
@Service
public class TmcCommunityActivityServiceImpl implements ITmcCommunityActivityService {
    @Autowired
    private TmcCommunityActivityMapper tmcCommunityActivityMapper;

    @Autowired
    private TmcActivityUserMapper tmcActivityUserMapper;


    /**
     * 小程序查询活动详情信息
     *
     * @param id 活动信息ID
     * @return 活动信息
     */
    @Override
    public TmcCommunityActivity selectActivityById(Long id, Long publicuserId) {
        TmcCommunityActivity tmcCommunityActivity = tmcCommunityActivityMapper.selectTmcCommunityActivityById(id);
        //查询活动参与人
        List<TmcUser> tmcUsers = tmcCommunityActivityMapper.selectPerson(id);
        //查询是否已参与活动
        HashMap map = new HashMap();
        map.put("id", id);
        map.put("publicuserId", publicuserId);
        if (tmcCommunityActivity.getPersoncount() == tmcUsers.size() && tmcCommunityActivity.getPersoncount() != 0) {
            tmcCommunityActivity.setState(2L);
        }
        if (tmcCommunityActivityMapper.selectCommunityActivityById(map) > 0) {
            tmcCommunityActivity.setState(3L);
        }
        tmcCommunityActivity.setPerson(tmcUsers.size());
        return tmcCommunityActivity;
    }

    public TmcCommunityActivity selectTmcCommunityActivityById(Long id) {
        return tmcCommunityActivityMapper.selectTmcCommunityActivityById(id);
    }

    /**
     * 查询活动信息列表
     *
     * @param tmcCommunityActivity 活动信息
     * @return 活动信息
     */
    @Override
    public List<TmcCommunityActivity> selectTmcCommunityActivityList(TmcCommunityActivity tmcCommunityActivity) {
        return tmcCommunityActivityMapper.selectTmcCommunityActivityList(tmcCommunityActivity);
    }

    /**
     * 新增活动信息
     *
     * @param tmcCommunityActivity 活动信息
     * @return 结果
     */
    @Override
    public int insertTmcCommunityActivity(TmcCommunityActivity tmcCommunityActivity) {
        //获取当前时间
        Calendar calendar = Calendar.getInstance();
        tmcCommunityActivity.setCreatetime(calendar.getTime());
        tmcCommunityActivityMapper.insertTmcCommunityActivity(tmcCommunityActivity);
        return tmcCommunityActivity.getId().intValue();
    }

    /**
     * 修改活动信息
     *
     * @param tmcCommunityActivity 活动信息
     * @return 结果
     */
    @Override
    public int updateTmcCommunityActivity(TmcCommunityActivity tmcCommunityActivity) {
        return tmcCommunityActivityMapper.updateTmcCommunityActivity(tmcCommunityActivity);
    }

    /**
     * 批量删除活动信息
     *
     * @param ids 需要删除的活动信息ID
     * @return 结果
     */
    @Override
    public int deleteTmcCommunityActivityByIds(Long[] ids) {
        return tmcCommunityActivityMapper.deleteTmcCommunityActivityByIds(ids);
    }

    /**
     * 删除活动信息信息
     *
     * @param id 活动信息ID
     * @return 结果
     */
    @Override
    public int deleteTmcCommunityActivityById(Long id) {
        return tmcCommunityActivityMapper.deleteTmcCommunityActivityById(id);
    }

    @Override
    public List<TmcUser> selectPerson(Long id) {
        return tmcCommunityActivityMapper.selectPerson(id);
    }

    @Override
    public Integer signUp(TmcActivityUser tmcActivityUser) {
        //获取当前时间
        Calendar calendar = Calendar.getInstance();
        tmcActivityUser.setCreatetime(calendar.getTime());
        tmcCommunityActivityMapper.signUp(tmcActivityUser);
        return tmcActivityUser.getId().intValue();
    }
//用户参加的所有活动
    @Override
    public List<TmcCommunityActivity> selectParticipate(Long publicuserId) {
        List<TmcCommunityActivity> tmcCommunityActivities = tmcCommunityActivityMapper.selectParticipate(publicuserId);
        for (int i = 0; i < tmcCommunityActivities.size(); i++) {
            List<TmcUser> tmcUsers = tmcCommunityActivityMapper.selectPerson(tmcCommunityActivities.get(i).getId());
            tmcCommunityActivities.get(i).setPerson(tmcUsers.size());
        }
        return tmcCommunityActivities;
    }

    @Override
    public Integer selectStatus(TmcActivityUser tmcActivityUser) {
        return tmcCommunityActivityMapper.selectStatus(tmcActivityUser);
    }

    @Override
    public List<TmcCommunityActivity> queryActivity() {
        //循环查询参与人数
        List<TmcCommunityActivity> tmcCommunityActivities = tmcCommunityActivityMapper.queryActivity();
        for (int i = 0; i < tmcCommunityActivities.size(); i++) {
            //声明状态字段
            Long state;
            //参与人数字段赋值
            List<TmcUser> tmcUsers = tmcCommunityActivityMapper.selectPerson(tmcCommunityActivities.get(i).getId());
            //是否超出报名时间
            Calendar calendar = Calendar.getInstance();
            if (calendar.getTime().compareTo(tmcCommunityActivities.get(i).getDeadlinetime()) >= 0) {
                state = 1L;
            } else {
                state = 0L;
            }
            //修改状态
            TmcCommunityActivity tmcCommunityActivity = new TmcCommunityActivity();
            tmcCommunityActivity.setId(tmcCommunityActivities.get(i).getId());
            tmcCommunityActivity.setState(state);
            tmcCommunityActivityMapper.updateTmcCommunityActivity(tmcCommunityActivity);
            //给各个活动赋值
            tmcCommunityActivities.get(i).setPerson(tmcUsers.size());
            tmcCommunityActivities.get(i).setState(state);
        }
        return tmcCommunityActivities;
    }

    @Override
    public Integer deletePerson(Long id, Long publicuserId) {
        HashMap map = new HashMap();
        map.put("id", id);
        map.put("publicuserId", publicuserId);
        TmcActivityUser tmcActivityUser = tmcCommunityActivityMapper.selectActivityById(map);
        if (tmcActivityUser.getState()==0){
            return tmcCommunityActivityMapper.deletePerson(map);
        }else {
            tmcActivityUser.setState(2L);
            return tmcActivityUserMapper.updateTmcActivityUser(tmcActivityUser);
        }
    }


    //查询活动
    @Override
    public TmcActivityUser selectActivityUser(Long id) {

        return tmcCommunityActivityMapper.selectActivityUser(id);
    }


    //查询费用
    @Override
    public int selectFeeById(Long activityId) {
        return tmcCommunityActivityMapper.selectFeeById(activityId);
    }
}
